package cn.explink.dop.base;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.explink.dop.entity.order.StoDelEntity;
import cn.explink.dop.service.StoDelService;
import cn.explink.eap.core.common.model.json.AjaxJson;
import cn.explink.eap.web.cgform.controller.autolist.CgAutoListController;

/**
 * 业务员&店面控制器
 * @author firefly 2015-2-28 15:10
 *
 */
@Controller
@RequestMapping("/stoDelController")
public class StoDelController extends CgAutoListController {
	
	@Autowired
	private StoDelService stoDelService;
	
	/**
	 * 基本信息
	 * @param request 请求
	 * @return 基本信息页面
	 */
	@RequestMapping(params = "index")
	public ModelAndView storefrontIndex(HttpServletRequest request) {
		ModelAndView view = new ModelAndView("dop/stodel/stodel");
		return view;
	}
	
	@RequestMapping(params = "create")
	@ResponseBody
	public AjaxJson saveBaseInfo(long restaurantId, long deliverymanId, HttpServletRequest request) {
		StoDelEntity sd = new StoDelEntity();
		sd.setRestaurantId(restaurantId);
		sd.setDeliverymanId(deliverymanId);
		StoDelEntity result = stoDelService.add(sd);
		AjaxJson j = new AjaxJson();
		if (null != result)
			j.setSuccess(true);
		return j;
	}
	
	@RequestMapping(params = "modify")
	@ResponseBody
	public AjaxJson updateData(long restaurantId, long deliverymanId, HttpServletRequest request) {
		StoDelEntity sd = new StoDelEntity();
		sd.setRestaurantId(restaurantId);
		sd.setDeliverymanId(deliverymanId);
		stoDelService.modify(sd);
		AjaxJson j = new AjaxJson();
		j.setSuccess(true);
		return j;
	}
	
	/**
	 * 获取数据
	 * @param pageNum 当前页
	 * @param pageSize 当前页条数
	 * @param request 请求
	 * @return 结果集
	 */
	@RequestMapping(params="loadData")
	@ResponseBody
	@SuppressWarnings("unchecked")
	public List<StoDelEntity> loadData(int pageNum, int pageSize, HttpServletRequest request) {
		Map<String, Object> map = stoDelService.getListDataByPage(new StoDelEntity(), pageNum, pageSize);
		return (List<StoDelEntity>) map.get("list");
	}
	
	/**
	 * 获取数据
	 * @param pageNum 当前页
	 * @param pageSize 当前页条数
	 * @param request 请求
	 * @return 结果集
	 */
	@RequestMapping(params="findData")
	@ResponseBody
	public List<StoDelEntity> findData(HttpServletRequest request) {
		List<StoDelEntity> list = stoDelService.getListData(new StoDelEntity());
		return list;
	}
	
	/**
	 * 获取查询数据
	 * @param pageNum 当前页
	 * @param pageSize 当前页条数
	 * @param request 请求
	 * @return 结果集
	 */
	@RequestMapping(params="queryList")
	@ResponseBody
	public Map<String, Object> queryData(long restaurantId, long deliverymanId, int pageNum, int pageSize, HttpServletRequest request) {
		StoDelEntity sd = new StoDelEntity();
		if(0 != restaurantId)
			sd.setRestaurantId(restaurantId);
		if(0 != deliverymanId)
			sd.setDeliverymanId(deliverymanId);
		return stoDelService.getListDataByPage(sd, pageNum, pageSize);
	}
	
	/**
	 * 删除数据
	 * @param rowList 数据字符串
	 * @param request 求情
	 * @return 删除状态
	 */
	@RequestMapping(params="delete")
	@ResponseBody
	@SuppressWarnings("unchecked")
	public AjaxJson deleteRows(String rowList, HttpServletRequest request) {
		 JSONArray json = JSONArray.fromObject(rowList);
		 List<StoDelEntity> list = JSONArray.toList(json, StoDelEntity.class);
		 AjaxJson j = new AjaxJson();
		 if (list.size() == stoDelService.deleteData(list))
			 j.setSuccess(true);
		return j;
	}
}
